import axios from 'axios';
import router from './router';
import ElementUI from 'element-ui';

// axios 配置
axios.defaults.timeout = 30000000;
// axios.defaults.baseURL = 'https://api.github.com';

// http request 拦截器
axios.interceptors.request.use(
  config => {
    // if (localStorage.user) { //判断token是否存在
    //   config.headers.common = {
    //     'X-Allow-Account': JSON.parse(window.localStorage.getItem('user')).accountId,
    //     'X-Allow-Token': JSON.parse(window.localStorage.getItem('user')).token
    //   }
    // }
    return config;
  },
  err => {
    return Promise.reject(err);
  }
);

// http response 拦截器
axios.interceptors.response.use(
  response => {
    // console.log(response)
    // if (response.data.code === 601) {
    //   sessionStorage.clear();
    //   localStorage.clear();
    //   ElementUI.Message.warning({
    //     message: response.data.msg || '服务器开小差啦',
    //     type: "warning"
    //   });
    //   router.replace('/login');
    // }
    return response;
  },
  error => {
    console.log(error)
    // return Promise.reject(error);
    switch (error.response.status) {
      case 403:
        ElementUI.Message.warning({
          message: "抱歉，您无权访问该接口",
          type: "warning"
        });
        break;
      case 404:
        ElementUI.Message.warning({
          message: "抱歉，您访问的接口不存在",
          type: "warning"
        });
        break;
      case 500:
        ElementUI.Message.warning({
          message: "抱歉，服务器出错了",
          type: "warning"
        });
        break;
      case 504:
        ElementUI.Message.warning({
          message: "网络超时,请联系管理员",
          type: "warning"
        });
        break;
    }
  }
);
export default axios;